package com.wenguodong.demofirstt.controller;

import com.wenguodong.demofirstt.pojo.User;
import com.wenguodong.demofirstt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;

/*
 *@Time：2023/4/15
 *@Author：Jelly
 *此类为注册页（register.html）的功能实现
 */
@Controller
public class RegisterController {

    @Autowired
    private UserService userService;

    //register.html的注册按钮  post类型
    @PostMapping("/register")
    public String register(User user, Model model){
        int flag;
        //接收用户注册时填写的信息，并自动装填到user中
        //将user添加至数据库中
        if((userService.addUser(user))!= null){
            //获取上个步骤返回的状态码
            flag = 200;
        }else {
            flag = 300;
        }

        if(flag == 200) {
            //添加成功，返回到登录页面
            return "redirect:/login";
        }else {
            //添加失败，输出注册失败信息，并返回到注册页面
            model.addAttribute("msg","注册失败，请重新输入");
            return "redirect:/register";
        }

    }

}
